package com.it.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.it.entity.Case;
import com.it.entity.Order;
import com.it.entity.Product;
import com.it.entity.User;
import com.it.service.CaseService;
import com.it.service.OrderService;
import com.it.service.ProductService;
import com.it.service.UserService;
import com.it.util.ItdragonUtils;
import com.it.util.Result;
import com.it.util.ResultResponse;
import com.it.util.TableResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈实列接口〉<br>
 *
 * @author
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService OrderService;
    @Autowired
    private ItdragonUtils itdragonUtils;
    @Autowired
    private CaseService caseService;
    @Autowired
    private ProductService productService;

    @Autowired
    private UserService userService;


    /**
     * 管理界面跳转接口
     *
     * @param mv
     * @return
     */
    @RequestMapping("/index.do")
    public ModelAndView index(ModelAndView mv) {
        mv.setViewName("order/index");
        return mv;
    }

    /**
     * 管理界面列表数据异步加载接口
     *
     * @param entity
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @GetMapping("order.do")
    public TableResultResponse reloadTable(Order entity, int page, int limit) {
        List<Map<String, Object>> infoList = new ArrayList<>();
        Page<Order> pageInfo = OrderService.selectPage(entity, page, limit);
        for (Order record : pageInfo.getRecords()) {
            Map<String, Object> resultMap = new HashMap<>(16);
            resultMap.put("id", record.getId());
            resultMap.put("name", record.getName());
            resultMap.put("caseUuId", record.getCaseUuId());
            resultMap.put("userName", record.getUserName());
            resultMap.put("realName", record.getRealName());
            resultMap.put("price", record.getPrice());
            resultMap.put("state", record.getState());
            resultMap.put("time", record.getTime() == null ? "" : record.getTime().substring(0, 19));
            infoList.add(resultMap);
        }
        return Result.tableResule(pageInfo.getTotal(), infoList);
    }


    @ResponseBody
    @GetMapping("orderMyself.do")
    public TableResultResponse orderMyself(Order entity, int page, int limit) {
        List<Map<String, Object>> infoList = new ArrayList<>();
        User user = userService.selectByPrimaryKey(itdragonUtils.getSessionUserId());
        entity.setUserName(user.getUserName());
        Page<Order> pageInfo = OrderService.selectPage(entity, page, limit);
        for (Order record : pageInfo.getRecords()) {
            Map<String, Object> resultMap = new HashMap<>(16);
            resultMap.put("id", record.getId());
            resultMap.put("name", record.getName());
            resultMap.put("caseUuId", record.getCaseUuId());
            resultMap.put("userName", record.getUserName());
            resultMap.put("realName", record.getRealName());
            resultMap.put("price", record.getPrice());
            resultMap.put("state", record.getState());
            resultMap.put("time", record.getTime() == null ? "" : record.getTime().substring(0, 19));
            infoList.add(resultMap);
        }
        return Result.tableResule(pageInfo.getTotal(), infoList);
    }

    /**
     * 新增界面跳转接口
     *
     * @param mv
     * @return
     */
    @RequestMapping("/addPage.do")
    public ModelAndView addPage(ModelAndView mv) {
        List<Case> caseList = caseService.getList(new Case());
        mv.addObject("caseList", caseList);
        List<Product> productList = productService.getList(new Product());
        mv.addObject("productList", productList);
        mv.setViewName("order/addPage");
        return mv;
    }

    /**
     * 新增界面跳转接口
     *
     * @param mv
     * @return
     */
    @RequestMapping("/addItem.do")
    public ModelAndView addItem(ModelAndView mv) {
        List<Case> caseList = caseService.getList(new Case());
        mv.addObject("caseList", caseList);

        mv.setViewName("order/addItem");
        return mv;
    }

    /**
     * 开药
     *
     * @param caseId
     * @param num
     * @param productId
     * @return
     */
    @ResponseBody
    @PostMapping("/order.do")
    public ResultResponse insert(String caseId, Integer num, String productId) {
        Product product = productService.getOne(productId);
        Case aCase = caseService.getOne(caseId);
        Order order = new Order();
        order.setUserName(aCase.getUserName());
        order.setRealName(aCase.getName());
        order.setPrice(product.getDisPrice() * num);
        order.setName(product.getName() + "✖" + num);
        order.setCaseUuId(aCase.getUuId());
        order.setProductId(productId);
        boolean result = OrderService.insert(order);
        if (!result) {
            return Result.resuleError("新增失败");
        }
        return Result.resuleSuccess();
    }


    /**
     * 编辑界面跳转接口
     *
     * @param mv
     * @return
     */
    @RequestMapping("/editPage.do")
    public ModelAndView editPage(ModelAndView mv, String id) {
        Order order = OrderService.getOne(id);
        mv.addObject("order", order);
        mv.setViewName("order/editPage");
        return mv;
    }

    /**
     * 缴费
     *
     * @param userName
     * @return
     */
    @ResponseBody
    @PostMapping("/toPay.do")
    public ResultResponse edit(String userName) {
        boolean result = OrderService.editById(userName);
        if (!result) {
            return Result.resuleError("缴费失败");
        }
        return Result.resuleSuccess();
    }

    /**
     * 删除数据接口
     *
     * @param id
     * @return
     */
    @ResponseBody
    @DeleteMapping("/order.do")
    public ResultResponse delete(String id) {
        boolean result = OrderService.deleteById(id);
        if (!result) {
            return Result.resuleError("删除失败");
        }
        return Result.resuleSuccess();
    }
}